from sqlalchemy import select
from sqlalchemy.orm import Session
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker


def init_session():
    """
    初始化数据库连接
    """
    from config import server_config
    if server_config.session_factory is None:
        session_factory = sessionmaker(bind=server_config.db_engine, autoflush=False, autocommit=False)
        server_config.session_factory = scoped_session(session_factory)


def get_session() -> Session:
    """
    获取数据库连接
    """
    from config import server_config
    return server_config.session_factory()


db_session = lambda : get_session()


# def execute_select_sql(sql):
#     db_session().execute(select()).fetchall():